package com.example.lirisheng.springbootfinalexperiment.controller;

import com.example.lirisheng.springbootfinalexperiment.entity.PersonInformation;
import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import com.example.lirisheng.springbootfinalexperiment.pojo.RegistryEntity;
import com.example.lirisheng.springbootfinalexperiment.service.ResService;
import com.example.lirisheng.springbootfinalexperiment.util.GeneralUtil;
import com.example.lirisheng.springbootfinalexperiment.util.RespBean;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    ResService resService;
    protected  final Logger logger = LoggerFactory.getLogger(WeiResController.class);

    @ApiOperation(value = "住户注册", notes = "住户注册")
    @PostMapping("/registryUser")
    public RespBean registerResident(@RequestBody @Valid RegistryEntity registryEntity, BindingResult result){

        if (logger.isDebugEnabled()){
            logger.debug("entry registry");
        }

        //验证申请入库表单是否合法
        RespBean respBean1;
        if((respBean1= GeneralUtil.validateError(result))!=null){
            return  respBean1;
        }

        //检验输入的信息是否合法
        RespBean respBean=resService.judgeRegistryInfo(registryEntity);
        if (respBean.getStatus()!=500){
            return  respBean;

        }

        Map<String,Object> maps = new LinkedHashMap<String,Object>();
        Users users = new Users();
        System.out.println("sdfss");
        //shiyong
        users.setUsername(registryEntity.getUsername());
        users.setPassword(registryEntity.getPassword());
        users.setOpenId(registryEntity.getOpen_id());
        PersonInformation personInformation = new PersonInformation();
        personInformation.setName(registryEntity.getName());
        personInformation.setIdentity(registryEntity.getIdentity());
        personInformation.setHouseId(registryEntity.getHouseId());
        try{
            resService.registryResident(users,personInformation);
            return RespBean.ok("注册成功");
        }catch (Exception exception){
            System.out.println("messsage:"+exception.getMessage());
            return RespBean.error("注册失败");
        }
    }

    @GetMapping("/welcome")
    public  RespBean welecome(){
        System.out.println("entry welcome");
        return RespBean.ok("欢迎访问页面你好吗");
    }



}
